/*
 * author      : 吴中勤 
 * date        : 2018/9/10
 * description : moby - sap 映射关系
 */

package com.deepexi.adapter.converter.sap;

import com.deepexi.adapter.converter.sap.order.SapAdapterOrderLine;
import com.deepexi.adapter.facade.moby.MobyOrderLineSenseEnum;
import com.deepexi.adapter.facade.moby.request.MobyOrderLine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SapMobyMapService {

    private static final Logger logger = LoggerFactory.getLogger(SapMobyMapService.class);

    /** 获取 保养 行项目 */
    public List<MobyOrderLine> getMaintainLine(List<MobyOrderLine> mobyOrderLineList) {
        if (null == mobyOrderLineList || 0 == mobyOrderLineList.size()) return null;

        return mobyOrderLineList.stream()
                .filter(line -> MobyOrderLineSenseEnum.FREE_MAINTAIN_PRODUCT_TYPE.nameEquals(line.getProductType())
                        || MobyOrderLineSenseEnum.PRODUCT_TYPE_MAINTAIN.nameEquals(line.getProductType()))
                .collect(Collectors.toList());
    }


    /** 获取 车辆 行项目 */
    public MobyOrderLine getVehicleLine(List<MobyOrderLine> mobyOrderLineList) {
        for (MobyOrderLine line : mobyOrderLineList) {
            if (MobyOrderLineSenseEnum.PRODUCT_TYPE_NEW_CAR.nameEquals(line.getProductType()))
                return line;
        }
        return null;
    }

    /** 获取 车辆 行项目 */
    public SapAdapterOrderLine getVehicleLineFromRecord(List<SapAdapterOrderLine> lineList) {
        for (SapAdapterOrderLine line : lineList) {
            if (MobyOrderLineSenseEnum.PRODUCT_TYPE_NEW_CAR.nameEquals(line.getProductType()))
                return line;
        }
        return null;
    }

    /** 获取 精品 行项目 */
    public List<MobyOrderLine> getBoutiqueLine(List<MobyOrderLine> mobyOrderLineList) {
        return mobyOrderLineList.stream()
                .filter(line -> MobyOrderLineSenseEnum.PRODUCT_TYPE_BOUTIQUE.nameEquals(line.getProductType())
                        || MobyOrderLineSenseEnum.PRODUCT_TYPE_FREE_BOUTIQUE.nameEquals(line.getProductType()))
                .collect(Collectors.toList());
    }

    /** 获取 精品 行项目 */
    public List<SapAdapterOrderLine> getBoutiqueLineFromRecord(List<SapAdapterOrderLine> list) {
        return list.stream()
                .filter(line -> MobyOrderLineSenseEnum.PRODUCT_TYPE_BOUTIQUE.nameEquals(line.getProductType())
                        || MobyOrderLineSenseEnum.PRODUCT_TYPE_FREE_BOUTIQUE.nameEquals(line.getProductType()))
                .collect(Collectors.toList());
    }

    /** 获取 已confirmed 的 金融 行项目 */
    public MobyOrderLine getConfirmedLoanLine(List<MobyOrderLine> mobyOrderLineList) {
        for (MobyOrderLine line : mobyOrderLineList) {
            String productType = line.getProductType();
            if (MobyOrderLineSenseEnum.INSTALLMENT_PRODUCT_TYPE.nameEquals(productType) && line.isConfirmed())
                return line;
        }
        return null;
    }

    /** 获取 已confirmed 的 保险 项目行 */
    public List<MobyOrderLine> getConfirmedInsuranceLine(List<MobyOrderLine> mobyOrderLineList) {
        return mobyOrderLineList.stream()
                .filter(line -> MobyOrderLineSenseEnum.INSURANCE_PRODUCT_TYPE.nameEquals(line.getProductType()))
                .collect(Collectors.toList());
    }

    /** 是否车辆 订单行 */
    public boolean isVehicleLine(MobyOrderLine mobyLine) {
        return null != mobyLine
                && MobyOrderLineSenseEnum.PRODUCT_TYPE_NEW_CAR.nameEquals(mobyLine.getProductType());
    }

    /** 是否保险 订单行 */
    public boolean isInsuranceLine(MobyOrderLine mobyLine) {
        return null != mobyLine
                && MobyOrderLineSenseEnum.INSURANCE_PRODUCT_TYPE.nameEquals(mobyLine.getProductType());
    }

    /** 是否保养 订单行 */
    public boolean isMaintainLine(MobyOrderLine mobyLine) {
        if (null == mobyLine) return false;
        return MobyOrderLineSenseEnum.PRODUCT_TYPE_MAINTAIN.nameEquals(mobyLine.getProductType())
                || MobyOrderLineSenseEnum.FREE_MAINTAIN_PRODUCT_TYPE.nameEquals(mobyLine.getProductType());
    }

    /** 是否免费保养 订单行 */
    public boolean isFreeMaintainLine(MobyOrderLine mobyLine) {
        return null != mobyLine
                && MobyOrderLineSenseEnum.FREE_MAINTAIN_PRODUCT_TYPE.nameEquals(mobyLine.getProductType());
    }

    /** 是否精品 订单行 */
    public boolean isBoutiqueLine(MobyOrderLine mobyLine) {
        if (null == mobyLine) return false;
        return MobyOrderLineSenseEnum.PRODUCT_TYPE_BOUTIQUE.nameEquals(mobyLine.getProductType())
                || MobyOrderLineSenseEnum.PRODUCT_TYPE_FREE_BOUTIQUE.nameEquals(mobyLine.getProductType());
    }

    /** 是否金融 订单行 */
    public boolean isLoanLine(MobyOrderLine mobyLine) {
        return null != mobyLine
                && MobyOrderLineSenseEnum.INSTALLMENT_PRODUCT_TYPE.nameEquals(mobyLine.getProductType());
    }


    /** 是否保险 订单行 */
    public boolean isServiceLine(MobyOrderLine mobyLine) {
        return null != mobyLine
                && MobyOrderLineSenseEnum.SERVICE_PRODUCT_TYPE.nameEquals(mobyLine.getProductType());
    }

    /** 判断某个订单行 是否免费 */
    public boolean isLineFree(MobyOrderLine mobyLine) {
        if (null == mobyLine) return false;
        return MobyOrderLineSenseEnum.FREE_MAINTAIN_PRODUCT_TYPE.nameEquals(mobyLine.getProductType())
                || MobyOrderLineSenseEnum.PRODUCT_TYPE_FREE_BOUTIQUE.nameEquals(mobyLine.getProductType());
    }
}
